﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class Flame : SceneMonoBase
{
    private GameObject BurnObj;
    private bool CanBurnOthers;
    public override void Awake()
    {
        base.Awake();
    }
    public override void OnEnable()
    {
        base.OnEnable();
        StopAllCoroutines();
        StartCoroutine(Init());
    }
    ParticleSystem.EmissionModule em;
    IEnumerator Init()
    {
        yield return 0;
        CheckPlayerIsIn();
        StartBurn();
        GameMgr.Get<ITimeManager>().RgstTimeAction(Data.Instance.TableAgent.GetFloat("Base", "AttrBurnTime", "Value"),()=> { EndBurn(); });
    }
    private void CheckPlayerIsIn()
    {
        foreach (GameObject obj in GameMgr.Get<IMonoManager>().GetAllMono()._ObjList.Values)
        {
            if ((GameMgr.Get<IMonoManager>().GetMono(obj.name).Position - sceneMonoData.position).magnitude < GetComponent<CircleCollider2D>().radius)
            {
                PushOthers(obj, (GameMgr.Get<IMonoManager>().GetMono(obj.name).Position - sceneMonoData.position).normalized, 0.1f, 5);
            }
        }
    }
    private void PushOthers(GameObject obj, Vector3 Direct, float Speed, int Frame)
    {
        obj.GetComponent<MonoBase>().LastTimeMove(Direct, Speed, Frame, () => { });
    }
    private void StartBurn()
    {
        if (sceneMonoData.attr == MonoAttribute.Dark || sceneMonoData.attr == MonoAttribute.Shine || sceneMonoData.attr == MonoAttribute.None)
        {
            GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
            StopAllCoroutines();
            return;
        }
        BurnObj = ResourceManager.Instance.Instantiate("Prefabs/Effects/Flame_" + GameMgr.Get<IMonoManager>().CurrentAttrName(sceneMonoData.attr));
        BurnObj.transform.position = transform.position;
        em = BurnObj.GetComponent<ParticleSystem>().emission;
        em.enabled = true;
        CanBurnOthers = true;
        GameMgr.Get<ISceneMonoManager>().SetFlameRecorder(gameObject);
    }
    public void EndBurn()
    {
        if (!gameObject.activeInHierarchy)
        {
            return;
        }
        if(sceneMonoData == null)
        {
            return;
        }
        CanBurnOthers = false;
        GameMgr.Get<ISceneMonoManager>().RemoveFromFlameRecorder(gameObject);
        em = BurnObj.GetComponent<ParticleSystem>().emission;
        em.enabled = false;
        StartCoroutine(DestroyMono());
    }
    IEnumerator DestroyMono()
    {
        yield return new WaitForSeconds(1);
        if (BurnObj != null)
        {
            ObjectPool.Recycle(BurnObj);
        }
        GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
    }
    public override void LateUpdate()
    {
        base.LateUpdate();
    }
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (!CanBurnOthers)
        {
            return;
        }
        if (sceneMonoData == null)
        {
            return;
        }
        if (collision.GetComponent<MonoBase>() != null)
        {
            CanBurnOthers = false;
            GameMgr.Get<IBuffManager>().AddBuff(GameMgr.Get<IMonoManager>().GetMono(collision.gameObject.name), GetBuffID(sceneMonoData.attr), 1);
            EndBurn();
            return;
        }
        if (collision.GetComponent<BulletBase>() != null)
        {
            if(collision.GetComponent<BulletBase>().bulletBase == null)
            {
                return;
            }
            if (collision.GetComponent<BulletBase>().bulletBase.attribute == MonoAttribute.None)
            {
                return;
            }
            if (collision.GetComponent<BulletBase>().bulletBase.attribute == sceneMonoData.attr)
            {
                collision.GetComponent<BulletBase>().HitNoneMono();
                collision.GetComponent<BulletBase>().OnCollide(gameObject);
                return;
            }
            CanBurnOthers = false;
            GameMgr.Get<IBuffManager>().AddBuff(sceneMonoData, GetBuffID(collision.GetComponent<BulletBase>().bulletBase.attribute));
            collision.GetComponent<BulletBase>().OnCollide(gameObject);
            EndBurn();
            return;
        }
    }
    private int GetBuffID(MonoAttribute Attr)
    {
        int buffID = 0;
        switch (Attr)
        {
            case MonoAttribute.None:
                buffID = 0;
                break;
            case MonoAttribute.Fire:
                buffID = 1;
                break;
            case MonoAttribute.Ice:
                buffID = 2;
                break;
            case MonoAttribute.Shine:
                buffID = 3;
                break;
            case MonoAttribute.Dark:
                buffID = 4;
                break;
            case MonoAttribute.Steam:
                buffID = 5;
                break;
            case MonoAttribute.Electric:
                buffID = 6;
                break;
            case MonoAttribute.Oil:
                buffID = 7;
                break;
            default:
                break;
        }
        return buffID;
    }
}
